package com.fan.teamer.model.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

import com.fan.teamer.model.domain.Member;
import com.fan.teamer.model.domain.Message;

public class MessageDaoImpl implements MessageDao {

	private SessionFactory sessionFactory;

	/**
	 * Get Hibernate Session Factory
	 * 
	 * @return SessionFactory - Hibernate Session Factory
	 */
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	/**
	 * Set Hibernate Session Factory
	 * 
	 * @param SessionFactory
	 *            - Hibernate Session Factory
	 */
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public Message getMessage(int id) {
		Message message = (Message) sessionFactory.getCurrentSession().get(
				Message.class, id);
		return message;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Message> getAllMessages() {
		return sessionFactory.getCurrentSession().createCriteria(Message.class)
				.list();
	}

	@Override
	public int getSize() {
		return getAllMessages().size();
	}

	@Override
	public void addMessage(Message message) {
		sessionFactory.getCurrentSession().save(message);
	}

	@Override
	public void updateStock(Message message) {
		sessionFactory.getCurrentSession().update(message);
	}

	@Override
	public void deleteMessage(Message message) {
		sessionFactory.getCurrentSession().delete(message);
	}

	@Override
	public boolean hasMessage(int id) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public List<String> getColumnNames() {
		// TODO Auto-generated method stub
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Message> getMessagesBySender(Member member) {
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(
				Message.class);
		criteria.add(Restrictions.eq("sender", member.getEmail()));
		List<Message> messages = criteria.list();

		return messages;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Message> getMessagesByReceiver(Member member) {
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(
				Message.class);
		criteria.add(Restrictions.eq("receiver", member.getEmail()));
		List<Message> messages = criteria.list();

		return messages;
	}

}
